<?php
$queries = $data->get('queries');

function highlightSQL($query) {
    $query = preg_replace('/\b(SELECT|FROM|AS|LIMIT|ASC|COUNT|DESC|WHERE|LEFT JOIN|INNER JOIN|RIGHT JOIN|ORDER BY|GROUP BY|IN|LIKE|DISTINCT|DELETE|INSERT|INTO|VALUES)\b/i', '<span class="sql">\1</span>', $query);
    return $query;
}
?>

<div id="<?php echo $data->get('title'); ?>">
    <h2><?php echo $data->get('title'); ?></h2>
    <h3>Databases</h3>
    <?php if (count($data->get('databases')) > 0): ?>
    <table id="databaseTable">
        <tr>
            <th>No.</th>
            <th>DSN</th>
            <th>Message</th>
            <th>Number of queries</th>
            <th>Total time</th>
        </tr>
        <?php $totalTime = 0; ?>
        <?php $totalCount = 0; ?>
        <?php foreach ($data->get('databases') as $index=>$database): ?>
        <tr>
            <td><?php echo $index+1; ?>.</td>
            <td><?php echo $database->getDSN(); ?></td>
            <td><?php echo htmlspecialchars($database); ?></td>
            <td>
                <?php
                    $count = 0;
                    foreach ($queries as $query):
                        if ($query->getDSN() === $database->getDSN()):
                                $count++;
                        endif;
                    endforeach;
                    echo $count;
                    $totalCount += $count;
                ?>
            </td>
            <td>
                <?php
                    $time = 0;
                    foreach ($queries as $query):
                        if ($query->getDSN() === $database->getDSN()):
                                $time += $query->getTimeInSecond();
                        endif;
                    endforeach;
                    echo \DebugComponent\Renderer\Humanize::time($time);
                ?>
            </td>
        </tr>
        <?php $totalTime += $time; ?>
        <?php endforeach; ?>
        <tr>
            <th colspan="3">Total</th>
            <th><?php echo $totalCount; ?></th>
            <th><?php echo \DebugComponent\Renderer\Humanize::time($totalTime) ?></th>
        </tr>
    </table>
    <?php else: ?>
        <div class="info">No databases</div>
    <?php endif; ?>

    <h3>Queries</h3>
    <?php if (count($data->get('queries')) > 0): ?>
    <table id="queriesTable">
        <tr>
            <th>No.</th>
            <th>Query</th>
            <th>DSN</th>
            <th>Time</th>
        </tr>
        <?php foreach ($data->get('queries') as $index=>$query): ?>
        <tr>
            <td><?php echo $index+1; ?>.</td>
            <td><?php echo highlightSQL(htmlspecialchars($query)); ?></td>
            <td><?php echo $query->getDSN(); ?></td>
            <td><?php echo \DebugComponent\Renderer\Humanize::time($query->getTimeInSecond()); ?></td>
        </tr>
        <?php endforeach; ?>
    </table>

    <script type="text/javascript">
        var queriesTable = document.getElementById('queriesTable');
        var databaseTable = document.getElementById('databaseTable');
        var databaseRows = databaseTable.getElementsByTagName('tr');
        var dsnTable = {};
        for (var i = databaseRows.length-1; i >= 0; i--) {
            var cells = databaseRows[i].getElementsByTagName('td');
            if (cells.length < 2) {
                continue;
            }
            var dsn = cells[1].innerHTML;
            var queriesRows = queriesTable.getElementsByTagName('tr');
            var tableEl = null;
            for (var j = 0; j < queriesRows.length; j++) {
                if (queriesRows[j].getElementsByTagName('td').length < 3) {
                    continue;
                }
                if (queriesRows[j].getElementsByTagName('td')[2].innerHTML == dsn) {
                    var row = queriesRows[j].cloneNode(true);
                    row.removeChild(row.childNodes[5]);
                    if (!dsnTable[dsn]) {
                        dsnTable[dsn] = document.createElement('table');

                        var headerRow = document.createElement('tr');
                        headerRow.appendChild(document.createElement('th')).appendChild(document.createTextNode('No.'));
                        headerRow.appendChild(document.createElement('th')).appendChild(document.createTextNode('Query'));
                        headerRow.appendChild(document.createElement('th')).appendChild(document.createTextNode('Time'));
                        dsnTable[dsn].appendChild(headerRow);

                        var newRow = document.createElement('tr');
                        newRow.style.display = 'none';
                        databaseRows[0].parentNode.insertBefore(newRow, databaseRows[i].nextSibling);
                        var titleEl = databaseRows[i].getElementsByTagName('td')[1];
                        var title = titleEl.innerHTML;
                        while (titleEl.firstChild) {
                            titleEl.removeChild(titleEl.lastChild);
                        }
                        var link = titleEl.appendChild(document.createElement('a'));
                        link.appendChild(document.createTextNode(title + ' ' + String.fromCharCode(9654)));
                        link.setAttribute('href','#');
                        Event.add(link, 'click', function(e) {
                            var row = Event.getTarget(e).parentNode.parentNode.nextSibling;
                            row.style.display = row.style.display != 'none' ? 'none' : 'table-row';
                            if (row.style.display != 'none')  {
                                Event.getTarget(e).innerHTML = Event.getTarget(e).innerHTML.replace(String.fromCharCode(9654), String.fromCharCode(9660))
                            } else {
                                Event.getTarget(e).innerHTML = Event.getTarget(e).innerHTML.replace(String.fromCharCode(9660), String.fromCharCode(9654))
                            }
                        });
                        var newCell = newRow.appendChild(document.createElement('td'));
                        newCell.setAttribute('colspan', 5);
                        newCell.appendChild(dsnTable[dsn]);
                        dsnTable[dsn].appendChild(row);
                    } else {
                        dsnTable[dsn].appendChild(row);
                    }
                }
            }
        }
    </script>

    <?php else: ?>
        <div class="info">No queries</div>
    <?php endif; ?>
</div>